﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Psm.Messaging;
using Psm.Util;

namespace Psm.PubSub.Messaging
{
    [Export("PUBSUB_MESSAGE_FACTORY_INTERNAL", typeof(IMessageFactory))]
    public class PubSubMessageFactoryInternal : IMessageFactory
    {
        public IMessage CreateMessage(ByteBuffer payload)
        {
            switch (payload.GetBuffer()[payload.StartIndex])
            {
                case (byte)PubSubMessageType.DATA_MESSAGE:
                    return new DataMessageInternal(payload);
                case (byte)PubSubMessageType.QUERY_MESSAGE:
                    return new QueryMessage(payload);
                case (byte)PubSubMessageType.SCHEMA_MESSAGE:
                    return new SchemaMessage(payload);
                default:
                    throw new Exception("Unrecognized Message");
            }
        }
    }
}
